'use strict';

const Controller = require('egg').Controller;

class AiAuditController extends Controller {
  /**
   * AI 智能审核游记
   * POST /api/ai/audit/:id
   * @param {number} id - 游记ID
   */
  async auditTravelNote() {
    const { ctx, service } = this;
    const id = parseInt(ctx.params.id);

    if (Number.isNaN(id)) {
      ctx.status = 400;
      ctx.body = { success: false, message: '无效的游记ID' };
      return;
    }

    try {
      const result = await service.aiAudit.auditTravelNote(id);

      if (result.success) {
        ctx.body = {
          success: true,
          data: result.data,
          message: 'AI 审核完成',
        };
      } else {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: result.message || 'AI 审核失败',
        };
      }
    } catch (error) {
      ctx.logger.error('AI 审核游记失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: 'AI 审核失败: ' + error.message,
      };
    }
  }

  /**
   * 批量 AI 审核游记
   * POST /api/ai/audit/batch
   * @param {Array<number>} ids - 游记ID数组
   */
  async batchAuditTravelNotes() {
    const { ctx, service } = this;
    const { ids } = ctx.request.body;

    if (!Array.isArray(ids) || ids.length === 0) {
      ctx.status = 400;
      ctx.body = { success: false, message: '请提供要审核的游记ID数组' };
      return;
    }

    try {
      const results = await service.aiAudit.batchAuditTravelNotes(ids);

      ctx.body = {
        success: true,
        data: results,
        message: `批量审核完成，成功 ${results.success} 个，失败 ${results.failed} 个`,
      };
    } catch (error) {
      ctx.logger.error('批量 AI 审核失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '批量 AI 审核失败: ' + error.message,
      };
    }
  }

  /**
   * AI 智能审核旅行计划
   * POST /api/ai/audit-plan/:id
   * @param {number} id - 旅行计划ID
   */
  async auditTravelPlan() {
    const { ctx, service } = this;
    const id = parseInt(ctx.params.id);

    if (Number.isNaN(id)) {
      ctx.status = 400;
      ctx.body = { success: false, message: '无效的旅行计划ID' };
      return;
    }

    try {
      const result = await service.aiAudit.auditTravelPlan(id);

      if (result.success) {
        ctx.body = {
          success: true,
          data: result.data,
          message: 'AI 审核完成',
        };
      } else {
        ctx.status = 400;
        ctx.body = {
          success: false,
          message: result.message || 'AI 审核失败',
        };
      }
    } catch (error) {
      ctx.logger.error('AI 审核旅行计划失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: 'AI 审核失败: ' + error.message,
      };
    }
  }

  /**
   * 批量 AI 审核旅行计划
   * POST /api/ai/audit-plan/batch
   * @param {Array<number>} ids - 旅行计划ID数组
   */
  async batchAuditTravelPlans() {
    const { ctx, service } = this;
    const { ids } = ctx.request.body;

    if (!Array.isArray(ids) || ids.length === 0) {
      ctx.status = 400;
      ctx.body = { success: false, message: '请提供要审核的旅行计划ID数组' };
      return;
    }

    try {
      const results = await service.aiAudit.batchAuditTravelPlans(ids);

      ctx.body = {
        success: true,
        data: results,
        message: `批量审核完成，成功 ${results.success} 个，失败 ${results.failed} 个`,
      };
    } catch (error) {
      ctx.logger.error('批量 AI 审核旅行计划失败:', error);
      ctx.status = 500;
      ctx.body = {
        success: false,
        message: '批量 AI 审核失败: ' + error.message,
      };
    }
  }
}

module.exports = AiAuditController;

